//
//  ViewController.m
//  BrowsePictureDemo
//
//  Created by GZMLUser on 14-11-13.
//  Copyright (c) 2014年 GZMLUser. All rights reserved.
//

#import "ViewController.h"
#import "UIImageView+MLWebCache.h"
#import "MLPhotoBrowser.h"
#import "MLPhoto.h"
@interface ViewController ()
{
    NSArray *_urls; // url数组
    
    NSMutableArray *_imagesPathArray;// 路径数组

    NSMutableArray *_photos;// 对象数组
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _photos = [NSMutableArray array];
    _urls = @[@"http://ww4.sinaimg.cn/thumbnail/7f8c1087gw1e9g06pc68ug20ag05y4qq.gif", @"http://ww3.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr0nly5j20pf0gygo6.jpg", @"http://ww4.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr1d0vyj20pf0gytcj.jpg", @"http://ww3.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr1xydcj20gy0o9q6s.jpg", @"http://ww2.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr2n1jjj20gy0o9tcc.jpg", @"http://ww2.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr39ht9j20gy0o6q74.jpg", @"http://ww3.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr3xvtlj20gy0obadv.jpg", @"http://ww4.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr4nndfj20gy0o9q6i.jpg", @"http://ww3.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr57tn9j20gy0obn0f.jpg"];
    
    _imagesPathArray = [NSMutableArray array];
    
    [self getImagesPath];
    
   // [self setLoacalData]; // 本地
    
    [self setNetWorkData]; // 网络
    
   // [self setMixData]; // 混合
}

#pragma mark 设置网络数据
- (void)setNetWorkData
{
    // 1.创建9个UIImageView
    UIImage *placeholder = [UIImage imageNamed:@"timeline_image_loading.png"];
    CGFloat width = 70;
    CGFloat height = 70;
    CGFloat margin = 20;
    CGFloat startX = (self.view.frame.size.width - 3 * width - 2 * margin) * 0.5;
    CGFloat startY = 50;
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    for (int i = 0; i<_urls.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        [scrollView addSubview:imageView];
       
        // 计算位置
        int row = i/2;
        int column = i%2;
        CGFloat x = startX + column * (width + margin);
        CGFloat y = startY + row * (height + margin);
        imageView.frame = CGRectMake(x, y, width, height);
        
        // 下载图片
        [imageView setImageURLStr:_urls[i] placeholder:placeholder];
        // 事件监听
        imageView.tag = i;
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapImage:)]];
        
        // 内容模式
        imageView.clipsToBounds = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        
        // photo对象设置
        MLPhoto *photo = [[MLPhoto alloc]init];
        photo.url = [NSURL URLWithString:[_urls[i] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"]] ;
        if (i%2 == 0) {
            // rect为对象在屏幕显示的位置
            CGRect rect = imageView.frame;
            rect.origin.y += 64;
            photo.rect = rect;
        }
        photo.placeholder = placeholder; // 设置站位图片
        [_photos addObject:photo];
        
    }
    scrollView.contentSize = CGSizeMake(0, 70 * 10);
    [self.view addSubview:scrollView];
}

- (void)getImagesPath // 获取图片路径保存到数组
{
    NSFileManager *fm = [NSFileManager defaultManager];
    // 文件夹路径
    NSString *totolPath = @"/Users/gzmluser/Desktop/参考资料/项目/BrowsePictureDemo/BrowsePictureDemo/MLBrowsePicture/lib/图片";
    NSArray *files = [fm contentsOfDirectoryAtPath:totolPath error:nil];
    for (NSString *file in files) {
        if ([file hasSuffix:@"jpg"]) {
            
            NSString *imagePath = [totolPath stringByAppendingPathComponent:file];
            
            // 添加
            [_imagesPathArray addObject:imagePath];
        }
    }
}

#pragma mark 设置本地数据
- (void)setLoacalData
{
    // 1.创建9个UIImageView
    CGFloat width = 70;
    CGFloat height = 70;
    CGFloat margin = 20;
    CGFloat startX = (self.view.frame.size.width - 3 * width - 2 * margin) * 0.5;
    CGFloat startY = 50;
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    for (int i = 0; i<_imagesPathArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.backgroundColor = [UIColor redColor];
        [scrollView addSubview:imageView];
    
        // 计算位置
        int row = i/2;
        int column = i%2;
        CGFloat x = startX + column * (width + margin);
        CGFloat y = startY + row * (height + margin);
        imageView.frame = CGRectMake(x, y, width, height);
        
        // 下载图片
        imageView.image = [UIImage imageWithContentsOfFile:_imagesPathArray[i]];
        // 事件监听
        imageView.tag = i;
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapImage:)]];
        
        // 内容模式
        imageView.clipsToBounds = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        
        // photo对象设置
        MLPhoto *photo = [[MLPhoto alloc]init];
        photo.path = _imagesPathArray[i];
       // photo.image = imageView.image;
        
        if (i%2 == 0) {
            // rect为对象在屏幕显示的位置
            photo.rect = imageView.frame;
        }else{
    
        }
        
        [_photos addObject:photo];
    }
    scrollView.contentSize = CGSizeMake(0, 70 * 10);
    [self.view addSubview:scrollView];
}

- (void)setMixData // 设置混合数据
{
    // 1.创建9个UIImageView
    UIImage *placeholder = [UIImage imageNamed:@"timeline_image_loading.png"];
    CGFloat width = 70;
    CGFloat height = 70;
    CGFloat margin = 20;
    CGFloat startX = (self.view.frame.size.width - 3 * width - 2 * margin) * 0.5;
    CGFloat startY = 50;
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    for (int i = 0; i<_imagesPathArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.backgroundColor = [UIColor redColor];
        [scrollView addSubview:imageView];
        
        // 计算位置
        int row = i/2;
        int column = i%2;
        CGFloat x = startX + column * (width + margin);
        CGFloat y = startY + row * (height + margin);
        imageView.frame = CGRectMake(x, y, width, height);

        // 事件监听
        imageView.tag = i;
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapImage:)]];
        
        // 内容模式
        imageView.clipsToBounds = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        
        // photo对象设置
        MLPhoto *photo = [[MLPhoto alloc]init];

        if (i%2 == 0) {
            [imageView setImageURLStr:_urls[i] placeholder:placeholder];
            // rect为对象在屏幕显示的位置
            photo.rect = imageView.frame;
            photo.url = [NSURL URLWithString:[_urls[i] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"]] ;
            
        }else{
            imageView.image = [UIImage imageWithContentsOfFile:_imagesPathArray[i]];
            // 可设置其中一种，也可混合设置
           // photo.path = _imagesPathArray[i];
            photo.image = imageView.image;
        }
        
        [_photos addObject:photo];
    }
    scrollView.contentSize = CGSizeMake(0, 70 * 10);
    [self.view addSubview:scrollView];

}


#pragma mark 图片点击监听
- (void)TapImage:(UITapGestureRecognizer *)tap
{
    // 2.初始相册及数据
    MLPhotoBrowser *bs = [[MLPhotoBrowser alloc]initWithImageArray:_photos currentTapedView:(UIImageView *)tap.view];
    // 设置状态栏及图片数量显示label
    [bs showPhotosWithStatusBarisHiden:YES indexLabelIsHidden:NO andSaveButtonIsHidden:NO];

}
@end
